package cn.bellychang.leetcode.question242;

import java.util.HashMap;
import java.util.Map;

/**
 * @author ChangLiang
 * @date 2021/3/16
 */
public class Solution {

    public boolean isAnagram(String s, String t) {

        HashMap<Character, Integer> hashMap1 = getHashMap(s);
        HashMap<Character, Integer> hashMap2 = getHashMap(t);
        for (Map.Entry<Character, Integer> entry : hashMap1.entrySet()) {
            Character key = entry.getKey();
            if (!hashMap1.get(key).equals(hashMap2.get(key))) {
                return false;
            } else {
                hashMap2.remove(key);
            }
        }
        if (hashMap2.size() != 0) {
            return false;
        }
        return true;
    }

    private HashMap<Character, Integer> getHashMap(String s) {
        HashMap<Character, Integer> hashMap = new HashMap<>(s.length());
        for (char c : s.toCharArray()) {
            if (hashMap.containsKey(c)) {
                hashMap.put(c, hashMap.get(c) + 1);
            } else {
                hashMap.put(c, 1);
            }
        }
        return hashMap;
    }
}
